﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Chaos.view.powersheet
{
    /// <summary>
    /// Interaction logic for powersheet.xaml
    /// </summary>
    public partial class powersheet : UserControl
    {
        public powersheet()
        {
            InitializeComponent();           
        }

        void model_powersheet_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Power")
            {
                ResetPowerMarker();
            }
        }

        private void ResetPowerMarker()
        {
            powermarker pm = null;
            for (int i = 0; i < powermarker_slots.Children.Count && pm == null; i++)
            {
                powerslot slot = powermarker_slots.Children[i] as powerslot;
                pm = slot.Slot as powermarker;
                if (pm != null)
                {
                    model.powersheet sheet = this.DataContext as model.powersheet;
                    slot.Slot = null;

                    slot = powermarker_slots.Children[sheet.Power] as powerslot;
                    slot.Slot = pm;
                    break;
                }
            }
        }

        private void powersheet_control_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            model.powersheet ps = this.DataContext as model.powersheet;
            if (ps != null)
            {
                ps.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(model_powersheet_PropertyChanged);
                ResetPowerMarker();
            }
        }
    }
}
